package com.lifp.controller;

import com.lifp.entity.LoginDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.passay.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @Description TODO
 * @Author lfp
 * @Date 2022/4/24 8:20
 * @Version 1.0
 */
@Api(tags = "passay 密码验证")
@RestController
@RequestMapping("passay")
public class PasswordValidateController {

    @ApiOperation("登录")
    @PostMapping("login")
    public Object login(LoginDTO loginDTO){
        PasswordValidator passwordValidator = new PasswordValidator(Arrays.asList(
                //长度规则，8-30
                new LengthRule(8,30),
                //字符规则 至少有一个大写字母
                new CharacterRule(EnglishCharacterData.UpperCase,1),
                //字符规则 至少有一个小写字母
                new CharacterRule(EnglishCharacterData.LowerCase,1),
                //字符规则 至少有一个特殊字符
                new CharacterRule(EnglishCharacterData.Special,1),
                //非法顺序规则 不允许有5个连续字母表顺序的字母，比如不允许abcde
                new IllegalSequenceRule(EnglishSequenceData.Alphabetical,5,false),
                //非法顺序规则 不允许有5个连续数字顺序的数字 比如不允许12345
                new IllegalSequenceRule(EnglishSequenceData.Numerical,5,false),
                //非法顺序规则 不允许有5个连续键盘顺序的字母 比如不允许asdfg
                new IllegalSequenceRule(EnglishSequenceData.USQwerty,5,false),
                //空格规则,不能有空格
                new WhitespaceRule()

        ));

        RuleResult ruleResult = passwordValidator.validate(new PasswordData(loginDTO.getPassword()));

        List<RuleResultDetail> details = ruleResult.getDetails();
        for (RuleResultDetail detail : details) {
            System.out.println(detail);
        }

        if(ruleResult.isValid()){
            return "登录成功";
        }else {
            return ruleResult.getDetails();
        }

    }

    @ApiOperation("修改密码")
    @PostMapping("update/password")
    public void updatePassword(){

    }
}
